package com.zcdyy.rentalofhouse.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.bean.OrderInfo;
import com.zcdyy.rentalofhouse.listener.OnItemClickListener;
import com.zcdyy.rentalofhouse.utils.ImageLoaderUtils;
import com.zcdyy.rentalofhouse.utils.Utils;

import java.util.List;

/**
 * Created by chuan on 2017/3/11.
 */

public class RentalAdapter extends RecyclerView.Adapter<RentalAdapter.MyViewHolder> {
    private List<OrderInfo> list;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public RentalAdapter(Context context, List<OrderInfo> list) {
        this.list = list;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void addList(List<OrderInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RentalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RentalAdapter.MyViewHolder holder = new RentalAdapter.MyViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_publishd_show, parent, false), onItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(RentalAdapter.MyViewHolder holder, int position) {
        ImageLoaderUtils.loadImage(context,list.get(position).getPic().getFileUrl(),holder.head);
        holder.title.setText(list.get(position).getTitle());
        holder.address.setText(list.get(position).getCity());
        holder.price.setText(list.get(position).getRent_money());
        holder.createTime.setText(list.get(position).getCreatedAt());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private ImageView head;
        private TextView title;
        private TextView address;
        private TextView price;
        private TextView createTime;

        public MyViewHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            head = Utils.findViewsById(itemView,R.id.header);
            title = Utils.findViewsById(itemView,R.id.tv_mess_name);
            address = Utils.findViewsById(itemView,R.id.tv_mess_qu);
            price = Utils.findViewsById(itemView,R.id.tv_rent_money);
            createTime = Utils.findViewsById(itemView,R.id.tv_createTime);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
